package com.gjzt.business.customer.service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.DateUtil;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.customer.inter.CustomerResourceMapper;
import com.gjzt.business.customer.model.CustomerResource;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerResourceService extends BaseService {

    @Autowired
    private CustomerResourceMapper customerResourceMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject findAdCustomerResource(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("", new Object[0]);
        return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 新增
     *
     * @param reqBean
     */
    public void addAdCustomerResource(ReqBean reqBean) {
        CustomerResource customerResource = getAdCustomerResource(reqBean);

        customerResourceMapper.insert(customerResource);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateAdCustomerResource(ReqBean reqBean) {
        String id = reqBean.getInfo("id");
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        CustomerResource customerResource = getAdCustomerResource(reqBean);
        customerResourceMapper.updateByPrimaryKeySelective(customerResource);
    }

    /**
     * 删除
     *
     * @param reqBean
     */
    public void removeAdCustomerResource(ReqBean reqBean) {
        String id = reqBean.getInfo("id");
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        customerResourceMapper.deleteByPrimaryKey(id);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private CustomerResource getAdCustomerResource(ReqBean reqBean) {
        CustomerResource customerResource = new CustomerResource();

        customerResource.setId(reqBean.getInfo("id"));
        customerResource.setCustomerId(reqBean.getInfo("customerId"));
        customerResource.setSource(reqBean.getInfo("source"));
        customerResource.setSourceBatchId(reqBean.getInfo("sourceBatchId"));
        customerResource.setResourceId(reqBean.getInfo("resourceId"));
        customerResource.setStartDate(DateUtil.parseDateTime(reqBean.getInfo("startDate")));
        customerResource.setEndDate(DateUtil.parseDateTime(reqBean.getInfo("endDate")));

        return customerResource;
    }

}